#ifndef SINGLETON_H
#define SINGLETON_H
#include "global.h"

/***************************************************************
* @file    singleton.h
* @brief   用来实现单例模式
*
* @author  轻语
* @date    2025/01/19
* @history
***************************************************************/

template <typename T>
class Singleton{
public:
    static std::shared_ptr<T> GetInstance(){
        static std::once_flag flag;
        std::call_once(flag,[&](){
           _instance = std::shared_ptr<T>(new T);//不用std::make_shared是因为构造函数被设置为protected了
        });
        return _instance;
    }
    ~Singleton(){
        qDebug()<<"~Singleton";
    }

    void PrintAddress(){
        qDebug()<<_instance.get();
    }
protected:
    Singleton() = default;
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
    static std::shared_ptr<T> _instance;
};

template <typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;

#endif // SINGLETON_H
